<template>
  <div class="container">
    <div class="titleInfo">
      <img class="sdLogo" src="https://www.bosssoft.com.cn/uploads/image/20220819/e3645974146074c25197a6753072dc0f.png" width="50" alt="">
      <div class="titleName">{{ systemName }}</div>
      <div class="operation">
        <a class="operaInfo">设为首页</a> |
        <a class="operaInfo" @click="CreateShortcut">收藏</a>
      </div>
    </div>
    <div class="login-container">
      <div class="title-container" />
      <el-form class="companyInfo-container">
        <text>JeeSite Demo</text>
      </el-form>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="chooseLogin">
          <div v-for="(item,index) in loginMethod" :key="index" class="chooseLoginItem" :class="{chooseLoginItemActive:loginChoose==index}" @click="loginChoose=index">{{ item }}</div>
        </div>
        <el-form-item prop="code">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="code"
            v-model="loginForm.code"
            placeholder="请输入工号"
            name="code"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item v-if="loginChoose==0" prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <!--      租户-->
        <el-form-item prop="tenant_id">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="account"
            v-model="loginForm.account"
            placeholder="租户id"
            name="account"
            type="text"
            tabindex="3"
            auto-complete="on"
          />
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click="handleLogin()">登录</el-button>
      </el-form>
    </div>
    <div class="webInfo">
      <div class="info-container">
        <div class="beian">闽ICP备17021229号-5(博思软件)</div>
        <div class="support">CopyRight &copy; 2008-2022  福建博思软件股份有限公司</div>
        <img class="code" src="../../assets/404_images/sd365Code.png" width="60" alt="" srcset="">
        <div class="support2">福建博思软件</div>
      </div>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui'
export default {
  name: 'Login',
  components: {
    // Slider
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        code: '100002',
        account: 'BOSSSOFT',
        password: 'sd365sd365'
      },
      loginRules: {
        code: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      systemName: '用户中心用户身份验证 v1.1.0-optimize', // 系统名称
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      loginMethod: ['账号登录'],
      loginChoose: 0,
      isShowSlider: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    CreateShortcut(url = location.href, title = document.title) {
      alert('快捷键Ctrl+D收藏为书签')
      document.dispatchEvent(new KeyboardEvent('keydown', { key: '17' }))
      document.dispatchEvent(new KeyboardEvent('keydown', { key: '68' }))
      return false
    },
    /**
     * 处理界面登录请求，请求将通过网关转发 用户中心 如果成功则返回200状态码 否则 401 可能是租户错误可能是账号密码错误
     */
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          /**
           * 调用后端 认证方法 完成用户认证 ，认证通过的条件为 账号 密码 和租户和用户表相等
           */
          this.$store.dispatch('user/login', this.loginForm).then(res => {
            if (res) {
              Message({
                message: '登陆成功',
                type: 'success',
                duration: 5 * 1000
              })
              // 路由跳转
              this.$router.push({ path: this.redirect || '/' })
            }
            this.loading = false
          }).catch(() => {
            Message({
              message: '输入的信息有误，认证失败',
              type: 'error',
              duration: 5 * 1000
            })
            this.loading = false
          })

          console.log(this.loginForm)
        } else {
          Message({
            message: '表单字段违反校验规则，为空或者不满足格式等',
            type: 'error'
            // duration: 5 * 1000
          })
          return false
        }
      })
    }

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$light_gray:rgb(0, 0, 0);
$cursor: rgb(0, 0, 0);

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  position: relative;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px white  inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.2);
    background: rgba(255, 255, 255);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$dark_gray:#889aa4;
$light_gray:#eee;

.container::-webkit-scrollbar {display:none}
.container{
  width: 100%;
  height: 100vh;
  overflow-x: hidden;
  overflow-y: scroll;
  -ms-overflow-style: none!important;
  overflow: -moz-scrollbars-none!important;
  .titleInfo{
    width: 100%;
    height: 8%;
    position: relative;
    .sdLogo{
      vertical-align: middle;
      text-align: center;
      width: 200px;
    }
    .titleName{
      display: inline-block;
      text-align: center;
      margin-left: 15px;
      line-height: 20px;
    }
    .operation{
      position: absolute;
      right: 10px;
      top: 20px;
      .operaInfo:hover{
        color: #409EFF;
      }
    }
  }
  .login-container {
    width: 100%;
    height: 80%;
    background-size: 110%;
    // background: #409eff;
    // background-image: linear-gradient(to top, #00c6fb 0%, #005bea 100%);
    // background-image: linear-gradient(to top, #fcc5e4 0%, #fda34b 15%, #ff7882 35%, #c8699e 52%, #7046aa 71%, #0c1db8 87%, #020f75 100%);
    background-image: linear-gradient(to top, #88d3ce 0%, #6e45e2 100%);
    overflow: hidden;
    .companyInfo-container{
    position: absolute;
    width: 400px;
    height: 400px;
    background:url("../../assets/permission-icon/comInfo.png") no-repeat;
    background-size: cover;
    box-sizing:border-box;
    max-width: 100%;
    padding: 20px 35px 0;
    top: 0;
    bottom: 0;
    left: 0;
    right: 520px;
    margin: auto;
    border-radius: 1em;
  }
    .login-form {
      position: absolute;
      width: 520px;
      height: 400px;
      max-width: 100%;
      padding: 20px 35px 0;
      top: 0;
      bottom: 0;
      left: 520px;
      right: 0;
      margin: auto;
      border-radius: 1em;
      // background-color: rgba(255,255,255,0.2);
      background-color: rgba(255,255,255);
      .el-input{
        background: white;
      }
      span{
        background: white;
      }

    }
    .tips {
      font-size: 14px;
      color: rgb(0, 0, 0);
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;
      margin-top:50px;

      .title {
        font-size: 26px;
        color: black;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: black;
      cursor: pointer;
      user-select: none;
    }
  }
  .webInfo{
    width: 100%;
    height: 12%;
    // background-color: #E4E7ED;
    background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    background-image: url('../../assets/permission-icon/wave.svg');
    // background-size: cover;
    background-size: 100%,80px;
    background-repeat: no-repeat;
    // background-position-y:46%;
    position: relative;
    margin:0;
    .info-container{
      position: absolute;
      width: 100%;
      left: 0;
      right: 0;
      bottom: 0;
      top: 0;
      margin:auto;
      .support{
        margin: 10px auto 5px;
        color: #687F7F;
        font-size: 13px;
        width: 300px;
      }
      .support2{
        position: absolute;
        bottom: 2px;
        right: 21px;
        font-size: 13px;
      }
      .beian{
        color: #687F7F;
        font-size: 13px;
        width:194px;
        margin: 25px auto 5px;
      }
      .suggest{
        color: red;
        margin: 10px auto 5px;
        width: 340px;
        font-size: 14px;
      }
      .code{
        position: absolute;
        top: 10px;
        right: 30px;
      }
    }
  }
}

.chooseLogin{
  color:black;
  display: flex;
  height:50px;
  .chooseLoginItem{
    line-height: 50px;
    padding:0 10px;
    cursor: pointer;
  }
  .chooseLoginItemActive{
    background-color: rgba(255, 255, 255);
    color: #000000;
  }
}
.drag {
  width: 300px;
  height: 40px;
  line-height: 40px;
  background-color: #e8e8e8;
  position: relative;
  margin: 0 auto;
}

.bg {
  width: 40px;
  height: 100%;
  position: absolute;
  background-color: #75CDF9;
}

.text {
  position: absolute;
  width: 100%;
  height: 100%;
  text-align: center;
  user-select: none;
}

.btn {
  width: 40px;
  height: 38px;
  position: absolute;
  border: 1px solid #ccc;
  cursor: move;
  font-family: "宋体";
  text-align: center;
  background-color: #fff;
  user-select: none;
  color: rgb(255, 255, 255);
}
</style>
